<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<fieldset class="fields-group flex flex-col" [formGroup]="entityTypesVersionLoadFormGroup">
  <legend class="group-title" translate>version-control.entities-to-restore</legend>
  <div class="flex flex-col" style="flex: 1; overflow: auto;">
    @for (entityTypeFormGroup of entityTypesFormGroupArray(); track entityTypeFormGroup; let index = $index, isLast = $last) {
      <div class="flex flex-row items-center justify-start" [class.pb-2]="!isLast">
        <mat-expansion-panel class="entity-type-config flex-1" [class.load]="loading" [formGroup]="entityTypeFormGroup"
                             [expanded]="entityTypesFormGroupExpanded(entityTypeFormGroup)" (opened)="loading = false">
          <mat-expansion-panel-header>
            <div class="flex flex-1 flex-row items-center justify-start">
              <mat-panel-title>
                <div class="flex flex-1 flex-row items-center justify-start">
                  <div>{{ entityTypeText(entityTypeFormGroup) }}</div>
                </div>
              </mat-panel-title>
              <span class="flex-1"></span>
              <button [class.hidden]="disabled" mat-icon-button style="min-width: 40px;"
                      type="button"
                      (click)="removeEntityType(index)"
                      matTooltip="{{ 'action.remove' | translate }}"
                      matTooltipPosition="above">
                <mat-icon>delete</mat-icon>
              </button>
            </div>
          </mat-expansion-panel-header>
          <div class="entity-type-config-content flex flex-col gap-2">
            <mat-divider></mat-divider>
            <div class="flex flex-row xs:flex-col gt-xs:gap-8">
              <tb-entity-type-select
                class="flex-1"
                showLabel
                formControlName="entityType"
                required
                appearance="outline"
                [filterAllowedEntityTypes]="false"
                [allowedEntityTypes]="allowedEntityTypes(entityTypeFormGroup)">
              </tb-entity-type-select>
              <div class="flex flex-1 flex-row xs:flex-col xs:gap-2 gt-xs:gap-4" formGroupName="config">
                <div class="flex flex-1 flex-col">
                  <mat-checkbox #removeOtherEntitiesCheckbox
                                formControlName="removeOtherEntities"
                                (change)="onRemoveOtherEntities(removeOtherEntitiesCheckbox, entityTypeFormGroup)">
                    {{ 'version-control.remove-other-entities' | translate }}
                  </mat-checkbox>
                  <mat-checkbox formControlName="findExistingEntityByName">
                    {{ 'version-control.find-existing-entity-by-name' | translate }}
                  </mat-checkbox>
                </div>
                <div class="flex flex-1 flex-col">
                  <mat-checkbox [class.hidden]="entityTypeFormGroup.get('entityType').value !== entityTypes.DEVICE" formControlName="loadCredentials">
                    {{ 'version-control.load-credentials' | translate }}
                  </mat-checkbox>
                  <mat-checkbox [class.hidden]="entityTypesWithoutRelatedData.has(entityTypeFormGroup.get('entityType').value)" formControlName="loadAttributes">
                    {{ 'version-control.load-attributes' | translate }}
                  </mat-checkbox>
                  <mat-checkbox [class.hidden]="entityTypesWithoutRelatedData.has(entityTypeFormGroup.get('entityType').value)" formControlName="loadRelations">
                    {{ 'version-control.load-relations' | translate }}
                  </mat-checkbox>
                  <mat-checkbox [class.hidden]="!typesWithCalculatedFields.has(entityTypeFormGroup.get('entityType').value)" formControlName="loadCalculatedFields">
                    {{ 'version-control.load-calculated-fields' | translate }}
                  </mat-checkbox>
                </div>
              </div>
            </div>
          </div>
        </mat-expansion-panel>
      </div>
    } @empty {
      <span translate class="tb-prompt flex items-center justify-center">version-control.no-entities-to-restore-prompt</span>
    }
  </div>
  <div style="padding-top: 16px;" class="flex flex-row">
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!addEnabled()"
            (click)="addEntityType()">
      <span translate>version-control.add-entity-type</span>
    </button>
    <span class="flex-1"></span>
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!entityTypesFormGroupArray().length"
            (click)="removeAll()">
      <span translate>version-control.remove-all</span>
    </button>
  </div>
</fieldset>
